const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
Page({

  data: {
    urls: 
    ['https://wx.qlogo.cn/mmopen/vi_32/PiajxSqBRaEIMmDuia5c9EKtmDYy4rpJvlF6aC0HIKIQmkibsIC9qkjep4BuOyGI9oCY1U9jCdJC9V4pBDhnwFjHA/0',
    'https://wx.qlogo.cn/mmopen/vi_32/PiajxSqBRaEIMmDuia5c9EKtmDYy4rpJvlF6aC0HIKIQmkibsIC9qkjep4BuOyGI9oCY1U9jCdJC9V4pBDhnwFjHA/0',
    'https://wx.qlogo.cn/mmopen/vi_32/PiajxSqBRaEIMmDuia5c9EKtmDYy4rpJvlF6aC0HIKIQmkibsIC9qkjep4BuOyGI9oCY1U9jCdJC9V4pBDhnwFjHA/0'],
    array: [
      { 'name': '未提交', state: 0 },
    ],
    index:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options);
    if (options.id) {
      var datas = {};
      datas.where = "AccID = " + options.id;
      var datacall = function (res) {
        console.log(res);
        if (res.data.data.length > 0) {
          var urls = [];
          var licence = [];
          licence[0] = res.data.data[0].LicenceImg;
          // urls[0] = res.data.data[0].LicenceImg;
          urls[0] = res.data.data[0].ShopImgUrl0;
          urls[1] = res.data.data[0].ShopImgUrl1;
          urls[2] = res.data.data[0].ShopImgUrl3;
          var mebid = wx.getStorageSync('mebid');
          var masterid = res.data.data[0].MasterMebID;
          var ismaster=false;
          if (mebid==masterid){
            ismaster=true;
          }
          that.setData({
            applyinfo: res.data.data[0],
            urls:urls,
            licence:licence,
            mebid: mebid,
            ismaster:ismaster
          })
          if (!options.type){
            that.getshoparray(mebid);
            that.setData({
              showmode: 0
            })
          } else{
            that.setData({
              showmode:1
            })
          }
        }
      }
      var res2 = post.getdata("/BO/TR_Account/Query", datas, datacall);
    }
  },
  getshoparray:function(mebid){
    var that = this;
    var datas = {};
    datas.where = "MebID = " + mebid;
    var datacall = function (res) {
      console.log(res);
      if (res.data.data.length > 0) {
        var obj = {};
        var arr = [];
        for (var i = 0; i < res.data.data.length;i++){
          obj.name=res.data.data[i].AccName;
          obj.id = res.data.data[i].DepID;
          obj.mebid = res.data.data[i].MebID;
          arr[i]=obj;
          obj={};
        }
        that.setData({
          array:arr
        })
      }
    }
    var res2 = post.getdata("/BO/AP_ShopMembers/Query", datas, datacall);
  },
  showimages:function(){
    wx.previewImage({
      current: '', // 当前显示图片的http链接
      urls: this.data.urls // 需要预览的图片http链接列表
    })
  },
  showlicence:function(){
    wx.previewImage({
      current: '', // 当前显示图片的http链接
      urls: this.data.licence // 需要预览的图片http链接列表
    })
  },
  bindPickerChange: function (e) {
    var that = this;
    console.log(that.data.array[e.detail.value])
    var depinfo = that.data.array[e.detail.value];
    that.setData({
      index: e.detail.value,
    })
    var datas = {};
    datas.where = "MebID = " + depinfo.mebid+" and DepID="+depinfo.id;
    var datacall = function (res) {
      console.log(res);
      if (res.data.data.length > 0) {
        wx.setStorageSync('newinfo', res.data.data[0]);
        wx.setStorageSync('depid', res.data.data[0].DepID);
        that.getinfo(res.data.data[0].DepID);
      }
    }
    var res2 = post.getdata("/BO/AP_ShopMembers/Query", datas, datacall);
  },
  //获取门店信息
  getinfo: function (depid){
    var that = this;
    var datas = {};
    datas.where = "AccID = " + depid;
    var datacall = function (res) {
      console.log(res);
      if (res.data.data.length > 0) {
        var urls = [];
        var licence = [];
        licence[0] = res.data.data[0].LicenceImg;
        urls[0] = res.data.data[0].ShopImgUrl0;
        urls[1] = res.data.data[0].ShopImgUrl1;
        urls[2] = res.data.data[0].ShopImgUrl3;
        var mebid = wx.getStorageSync('mebid');
        that.setData({
          applyinfo: res.data.data[0],
          urls: urls,
          licence: licence,
          mebid: mebid
        })
        if (res.data.data[0].MasterMebID == mebid) {
          that.getshoparray(mebid);
        }
      }
    }
    var res2 = post.getdata("/BO/TR_Account/Query", datas, datacall);
  },
  saoyisao: function () {
    var that = this;
    wx.scanCode({
      success: (res) => {
        console.log(res.result);
        var datas = res.result.split('&');
        var mebarr = datas[0].split(':');
        if (mebarr[0] == 'mebid' && 　mebarr[1] != '') {
          console.log(111);
          var datas = {};
          datas.where = "MebID = " + mebarr[1];
          var datacall = function (res) {
            console.log(res);
            if (res.data.data.length != 0) {
              if (!res.data.data[0].IsService) {
                wx.showModal({
                  title: '',
                  content: '对方不是客户经理，无法添加',
                })
                return;
              } else {
                console.log("oktian");
                that.addservice(res.data.data[0].MebID);
              }
            }
          }
          var res2 = post.getdata("/BO/BA_Member/Query", datas, datacall);

        } else {
          wx.showModal({
            title: '',
            content: '不是符合规定的二维码',
          })
        }
      }
    })
  },
  //添加客户经理
  addservice: function (id) {
    var that = this;
    var datas = {};
    var data = {};
    data.AccID = that.data.applyinfo.AccID;
    data.ServiceMebID = id;
    datas.data = JSON.stringify(data);
    var datacall = function (res) {
      console.log(res);
      var applyinfo = that.data.applyinfo;
      applyinfo.ServiceMebID = id;
      that.setData({
        applyinfo:applyinfo
      })
      wx.showModal({
        title: '',
        content: '添加客户经理成功',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/service/service?id=' + id
            })
          } else if (res.cancel) {
            return;
          }
        }
      })

    }
    var res2 = post.getdata("/BO/TR_Account/Modify", datas, datacall);
  },
  toservice: function () {
    var info = this.data.applyinfo.ServiceMebID;
    if (!info) {
      info = '';
    }
    wx.navigateTo({
      url: '/pages/service/service?id=' + info
    })

  },
  showson:function(){
    wx.navigateTo({
      url: '/pages/showson/showson',
    })
  }
})